package View;

import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.util.Vector;
import java.util.ListIterator;

import Controller.*;
import Model.*;
import Model.Construction.*;
import root.*;


/**
 * La class BoardPanel est un panel qui contient la terrain de la ville.
 * On y dessine les cases du terrain ainsi que les batiments de la ville
 *
 */


public class BoardPanel extends JPanel{

	private Ville ville;
	private Board board;

	private BufferedImage BIFondAnimation;
	private boolean selection;

	//Var de selection :
	public Point SupGauche, InfDroite;
	private boolean constructible;
	private String infra;

	public BoardPanel(int cote, Ville v) {
		ville = v;
		board = v.getBoard();
		setPreferredSize(new Dimension(cote,cote));
		setBackground(Color.lightGray);
		constructible = true;
	}

	//setters & getters

	public int getTailleTerrain() {
		//renvoi la taille en case du terrain
		return board.getTaille();
	}

	public void setListener(EcouteurDeBoard eboard) {
		addMouseMotionListener(eboard);
		addMouseListener(eboard);
	}

	public void setSelection(boolean s) {
		selection = s;
	}

	public void setInfra(String s) {
		infra = s;
	}

	public void setConstructible(boolean b) {
		constructible = b;
	}


	// methodes
	public void bufferiserPlateau() {
		BIFondAnimation = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_RGB);
		Graphics2D drawable = BIFondAnimation.createGraphics();
		dessineplateau(drawable);
	}


	public void dessineplateau(Graphics2D drawable) {
		int width = getSize().width;
		int height = getSize().height;

		// On efface tout
		drawable.setPaint(Color.white);
		drawable.fillRect(0, 0, width, height);


		//d'abord le fond
		Color c=null;
		try {
			c = MapObjects.getCouleur("Plateau");
		} catch (ColorNotFoundException e) {
			System.out.println(e);
		}
		for (int i=0; i<getTailleTerrain(); i++) {
			for (int j=0; j<getTailleTerrain(); j++) {
				drawable.setPaint(c);
				afficherCase(i,j,drawable);
			}
		}

		//ensuite les infrastructures presentes
		Vector<Infrastructure> vecteurInfra = ville.getVecteurInfra();
		ListIterator<Infrastructure> iter = vecteurInfra.listIterator();
		while (iter.hasNext()) {
			Infrastructure infra = iter.next();
			dessinerInfra(infra, drawable);
		}
	}

	public void afficherCase(int i, int j, Graphics2D drawable) {
		//dessin les case vierge
		Dimension p = getSize();
		int x,y;

		x = i*p.width/getTailleTerrain();
		y = j*p.height/getTailleTerrain();

		Paint paint = drawable.getPaint();
		int cote = p.height/getTailleTerrain();
		//grille
		drawable.setPaint(Color.gray);
		drawable.fillRect(x, y, cote, cote);

		//case reele
		if(!constructible) {
			drawable.setPaint(new Color(255,0,0,127));
		} else {
			drawable.setPaint(paint);
		}
		drawable.fillRect(x+1, y+1, cote-1, cote-1);
	}


	public void dessinerInfra(Infrastructure infra, Graphics2D drawable) {
		//dessine les batiments
		Dimension p = getSize();
		int x, y;

		x = infra.getPosX()*p.width/getTailleTerrain();
		y = infra.getPosY()*p.height/getTailleTerrain();

		int hauteur = (p.height/getTailleTerrain())*infra.getHauteur();
		int largeur = (p.height/getTailleTerrain())*infra.getLargeur();

		//grille
		drawable.setPaint(Color.gray);
		drawable.fillRect(x, y, largeur, hauteur);

		//case reele
		Color c=null;
		try {
				c = MapObjects.getCouleur(infra.getClass().getName());
		} catch (ColorNotFoundException e) {
				System.out.println("Boardpanel : "+e);
		}
		drawable.setPaint(c);
		drawable.fillRect(x+1, y+1, largeur-1, hauteur-1);
	}


	public void paintComponent(Graphics g) {
		//System.out.println("Repaint BoardPanel");
		Graphics2D drawable = (Graphics2D) g;

		// On reccupere quelques infos provenant de la partie JComponent
		int width = getSize().width;
		int height = getSize().height;

		if (! selection) {
			dessineplateau(drawable);
		} else {
			drawable.drawImage(BIFondAnimation, 0, 0, null);

			Color coul=null;
			try {
				coul = MapObjects.getCouleur(infra);
			} catch (ColorNotFoundException e) {
				System.out.println("Boardpanel : "+e);
			}
			drawable.setPaint(coul);
			for(int i=SupGauche.x; i<=InfDroite.x; i++) {
				for(int j=SupGauche.y; j<=InfDroite.y; j++) {
					afficherCase(i,j, drawable);
					//affectation dans le tableau du terrain : travail du controller !
				}
			}
		}
	}
}
